﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.Data.Providers.SqlServer.Query;

namespace NBusiness.Query.Testing
{
    [TestClass]
    class UpdateTests
    {
        [TestMethod]
        public void TestAssignment()
        {
            IEvaluatable left = new SqlColumn("A");
            IEvaluatable right = new SqlParameter("@A");
            IAssignment assignment = new SqlAssignment(left, right);

            string expected = "[A]=@A";
            string actual = assignment.ToSql();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void TestUpdateSuccess1()
        {
            ITable table = new SqlTable("T");
            IColumn c1 = new SqlColumn("C1", table);
            IColumn c2 = new SqlColumn("C2", table);
            IColumn c3 = new SqlColumn("C3", table);

            IQuery query = new SqlQuery();
            query.Update()
                .Table(table)
                .Set(c1, "@C1")
                .Set(c2, "@C2")
                .Where(c3.Equal("@C3"));

            string expected = "UPDATE [T] SET [C1]=@C1,[C2]=@C2 WHERE ([C3]=@C3)";
            string actual = query.ToSql();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestUpdateFail1_AssignmentsExpected()
        {
            IQuery query = new SqlQuery();
            query.Update().Table(new SqlTable("T"));
            string sql = query.ToSql();
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestUpdateFail2_AssignmentsExpected()
        {
            IQuery query = new SqlQuery();
            query.Update();
            string sql = query.ToSql();
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestUpdateFail3_AssignmentsExpected()
        {
            IQuery query = new SqlQuery();
            query.Update().Table(new SqlTable("T")).Where(new SqlColumn("C").Equal("C"));
            string sql = query.ToSql();
        }
    }
}
